const WpsCom = require("./wps_com");
const Login = require("./Login");
const WpsComSpecialGroup = require("./SpecialGroup").SpecialGroup;


class AccountManager {
  /**
   * 
   * @param {Array<{
   * account_code:String,
   * wps:WpsCom,
   * wps_sg:WpsComSpecialGroup,
   * login_info:{
   * mail:String,
   * pass:String
   * }
   * }>} accounts 
   */
  constructor(accounts) {
    this.accounts = accounts;
  }

  /**
   * @returns {{ok:Boolean,msg:String,data:{
   * wps:WpsCom,
   * wps_sg:WpsComSpecialGroup
   * }}} 
   * @param {String} ac_code 
   */
  get_by_account_code(ac_code) {
    let found = this.accounts.find(e => e.account_code == ac_code);
    if (!found) {
      return ({
        ok: false,
        msg: `找不到这个account_code(${ac_code})的账户啊`
      })
    }
    if (!(found.wps_sg && found.wps)) {
      return ({
        ok: false,
        msg: `account_code(${ac_code})里没有wps/wps_sg`
      })
    }
    return {
      ok: true,
      msg: "ok",
      data: {
        wps: found.wps,
        wps_sg: found.wps_sg
      }
    }
  }

  /**
   * @returns {Promise<{ok:Boolean,msg:String}>}
   * @param {String} account_code 
   * @param {String} wps_sid 
   */
  setWpsSid(account_code, wps_sid) {
    return new Promise(async resolve => {
      let foundIndex = this.accounts.findIndex(e => e.account_code == account_code);
      if (foundIndex <= 0) {
        return resolve({
          ok: false,
          msg: `无法找到account_code=${account_code}的记录`
        })
      }
      let wps = new WpsCom(wps_sid);
      let o_sg = await wps.GetSpecialGroupPromise;
      if (!o_sg.ok) {
        return resolve({
          ok: false,
          msg: `account_code=${account_code}::GetSpecialGroupPromise 失败了：：${o_sg.msg}`
        })
      }
      this.accounts[foundIndex].wps = wps;
      this.accounts[foundIndex].wps_sg = o_sg.data.special_group;
      return resolve({
        ok: true,
        msg: "ok"
      })
    })
  }
}


/**
 * @returns {Promise<{am:AccountManager,login_fail_msgs:String[]}>}
 * @param {Array<{mail:String,pass:String,define_account_code:String}>} account_infos 
 */
function initAccountManager(account_infos) {
  return new Promise(async resolve => {
    let login_promises = account_infos.map(e => Login.loginByAccountWpsCom(e.mail, e.pass));
    /**@todo 还没写完 解决多个账号登陆管理问题 要能手动设定cookie! */
    let o_all = await Promise.all(login_promises);
    /**@type {string[]} */
    let login_fail_msgs = [];


    /**@type {Array<{
   * account_code:String,
   * wps:WpsCom,
   * wps_sg:WpsComSpecialGroup,
   * login_info:{
   * mail:String,
   * pass:String
   * }
   * }>} */
    let accounts = [];

    for (let i in o_all) {
      if (!o_all[i].ok) {
        login_fail_msgs.push(`LOGIN FAIL.account_code=${account_infos[i].define_account_code},reason=${o_all[i].msg}`);
        accounts.push({
          account_code: account_infos[i].define_account_code,
          wps: null,
          wps_sg: null,
          login_info: {
            mail: account_infos[i].mail,
            pass: account_infos[i].pass
          }
        });
      } else {
        let wps = o_all[i].data.wpscom;
        let o_sg = await wps.GetSpecialGroupPromise;
        if (!o_sg.ok) {
          login_fail_msgs.push(`GET SPECIAL GROUP FAIL.account_code=${account_infos[i].define_account_code},reason=${o_sg.msg}`);
          accounts.push({
            account_code: account_infos[i].define_account_code,
            wps: null,
            wps_sg: null,
            login_info: {
              mail: account_infos[i].mail,
              pass: account_infos[i].pass
            }
          });
        } else {
          accounts.push({
            account_code: account_infos[i].define_account_code,
            wps: wps,
            wps_sg: o_sg.data.special_group,
            login_info: {
              mail: account_infos[i].mail,
              pass: account_infos[i].pass
            }
          })
        }
      }
    }

    let acc_man = new AccountManager(accounts);
    return resolve({
      am: acc_man,
      login_fail_msgs: login_fail_msgs
    })


  })
}



module.exports = {
  initAccountManager: initAccountManager
}